package cn.spring.cloud.activity.mybatisps.springbootrabbitmq.controller;

import cn.spring.cloud.activity.mybatisps.springbootrabbitmq.entity.User;
import cn.spring.cloud.activity.mybatisps.springbootrabbitmq.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2024-03-22 13:18:50
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;


    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping("/register")
    public String register(@RequestBody User user) {

         userService.insert(user);
         userService.sendMobileMessage("13993139592");
         rabbitTemplate.convertAndSend("mailExchange","user.mail",user.getUemail());
         return "注册成功";
    }



    /**
     *注册
     */
//    public ResultBean register(User user, String code) {
//        //判断redis中是否存在
//        String verifyCode = redisTemplate.opsForValue().get(verifyCodeKey + user.getMobile());
//        if (StringUtils.isEmpty(verifyCode)) {
//            return new ResultBean(new CustomerException("短信验证码为空"), -1);
//        }
//        if (!verifyCode.equals(code)) {
//            return new ResultBean(new CustomerException("验证码错误!"), 100);
//        }
//        save(user);
//        return new ResultBean();
//    }


    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<User> edit(User user) {
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }

}

